<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class User_m extends FOX_Model
{
    function __construct()
    {
        parent::__construct();
    }

	function get_user_by_user_id($user_id){
        $query = $this->db->select('*')->from('users')->where('user_id',$user_id)->get();
        $res = $query->row_array();
        return $res;
	}

	function get_user_by_user_key($user_key){
		$query = $this->db->select('*')->from('users')->where('user_key',$user_key)->get();
        return $query->row_array();
	}
	
	function get_user_by_user_id_group($user_id){
		$query = $this->db->select('a.*,b.*')->from('users a')->join('auth_groups b','b.id=a.user_group','LEFT')->where('a.user_id',$user_id)->get();
        return $query->row_array();
	}
	
	function get_user_by_username($user_name){
		$query = $this->db->select('*')->from('users')->where('user_name',$user_name)->get();
        return $query->row_array();
	}
	
	function get_user_by_user_group($user_group)
	{
		$query = $this->db->select('title')->from('auth_groups')->where('id',$user_group)->get();
        $info = $query->row_array();
		if($info){
			return $info['title'];
		}else{
			return '未知';
		}
		
	}

	function register($data){
		return $this->db->insert('users',$data);
	}

    function login($data){
	    $user = $this->check_register_username_or_tel($data['user_name']);
	    if($user){
			$password = password_dohash($data['user_password'],$user['user_salt']);
			if($user['user_password']==$password){			    
				$this->session->set_userdata('user_id',$user['user_id']);;
				$loveme = password_dohash($user['user_id'],$user['user_salt']);
				if($loveme){
					$_COOKIE['loveme'] = $loveme;
					setcookie('loveme', $loveme, time() + 86500, '/', '', false, true);
				}
                $this->user_m->update_user($user['user_id'],array('user_logtime'=>time(),'user_lognum'=>$user['user_lognum']+1));
				return TRUE;
			} else {
				return FALSE;
			}
	    } else {
			return FALSE;
		}
    }
	
	function checklogin($key,$salt){
		$user=$this->user_m->get_user_by_user_key($key);
		if($user){
			$this->session->set_userdata('user_id',$user['user_id']);;
			$loveme = password_dohash($user['user_id'],$salt);
			if($loveme){
				$_COOKIE['loveme'] = $loveme;
				setcookie('loveme', $loveme, time() + 86500, '/', '', false, true);
			}
		}
	}
	
	function check_ulock($username){
		$query = $this->db->select('user_lock')->from('users')->where('user_name',$username)->or_where('user_tel',$username)->get();
        $ulock = $query->row_array();
		if($ulock['user_lock']==0){
			return true;
		}else{
			return false;
		}
	}

	function check_register_username($user_name){
		$query = $this->db->select('user_lock')->from('users')->where('user_name',$user_name)->or_where('user_tel',$user_name)->get();
        return $query->row_array();
	}
	
	function update_user($uid, $data){
		$this->db->where('user_id',$uid);
  		$this->db->update('users', $data); 
		return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
	}
	
	public function update_ulognum($uid,$num=1)
	{
		$this->db->set('user_lognum','user_lognum+'.$num,false)->where('user_id',$uid)->update('users');
		return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
	}

	
	public function del_user_key_by_user_id($uid)
	{
        $user_key = password_dohash($uid,substr(uniqid(rand()), 6));
//		$this->db->set('user_key',$user_key,false)->where('user_id',$uid)->update('users');
        $this->user_m->update_user($uid,array('user_key'=>$user_key));
		return ($this->db->affected_rows() > 0) ? TRUE : FALSE;
	}

    function get_user_with_group($uid){
        $sql = "SELECT * FROM {$this->db->dbprefix}auth_groups g left join {$this->db->dbprefix}auth_group_access a on g.id=a.group_id where a.uid=$uid";
        $query=$this->db->query($sql);
        return $query->row_array();
    }

    public function get_user_group_rule_list($limit,$start,$key){
        $this->db->select('users.*,auth_groups.id as gid,auth_groups.title');
        $this->db->from('fox_users');
        $this->db->join('auth_group_access','users.user_id = auth_group_access.uid','left');
        $this->db->join('auth_groups','auth_groups.id = auth_group_access.group_id','left');
        $this->db->limit($start,$limit);
        if($key){
            if(is_numeric($key)){
                $this->db->where('user_id', $key);
                $this->db->or_where('user_tel', $key);
            }else{
                $this->db->like('user_name', $key);
                $this->db->or_like('user_tel', $key);
            }
        }
        $this->db->where('user_group !=',1);
        $this->db->where('delete',0);
        $this->db->order_by('user_id','desc');
        $query = $this->db->get();
        if($query->num_rows() > 0){
            return $query->result_array();
        }

    }
    //计算关键字的用户
    public function count_user_find($key)
    {
        $this->db->select('user_id');
        if($key){
            $this->db->like('user_name',$key);
            $this->db->like('user_tel',$key);
        }
        $this->db->from('users');
        // $this->db->where('good_lock',0);
        $total=$this->db->count_all_results();
        return $total;
    }

    /**
     * 判断号码是否已注册
     * @param $mobile
     * @return bool
     */
    public function is_mobile_registered($mobile){

        $this->db->select('*');
        $this->db->where("user_tel",$mobile);
        $this->db->from('users');
        $query = $this->db->get();
        return $query->row_array();
    }


    public function get_username_by_utel($utel){
        $query = $this->db->select('user_name')->from('users')->where('user_tel',$utel)->get();
        return $query->row_array();
    }


    function check_register_usertel($user_tel){
        $query = $this->db->select('*')->from('users')->where('user_tel',$user_tel)->get();
        if($query->num_rows() > 0){
            return $query->row_array();
        }
    }

    function check_register_username_or_tel($user_name){
        $query = $this->db->select('*')->from('users')->where('user_name',$user_name)->or_where('user_tel',$user_name)->get();
        return $query->row_array();
    }

    public function get_users(){
        $this->db->select('*');
        $this->db->from('users');
        $this->db->where('user_group',4);
        $this->db->where('delete',0);
        $query = $this->db->get();
        return $query->result_array();
    }

}
